package com.lagou.edu.controller;

import com.lagou.edu.pojo.LagouToken;
import com.lagou.edu.pojo.LagouUser;
import com.lagou.edu.service.CodeFeignClient;
import com.lagou.edu.service.LagouTokenService;
import com.lagou.edu.service.LagouUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

@RestController
@RequestMapping("/api/user")
@Slf4j
public class UserController {
    @Autowired
    private LagouTokenService tokenService;

    @Autowired
    private LagouUserService userService;

    @Autowired
    private CodeFeignClient codeFeignClient;

    /**
     * 注册接口
     *
     * @param email
     * @param password
     * @param code
     * @return
     */
    @GetMapping("/register/{email}/{password}/{code}")
    public Integer register(@PathVariable String email,
                            @PathVariable String password,
                            @PathVariable String code) {

        // 验证code是否正确
        Boolean pass = codeFeignClient.validate(email, code);
        if (pass) {
            // 判断该邮箱是否已经注册
            LagouUser userInfo = userService.findOne(email);
            if (userInfo == null) {
                // 进行注册
                LagouUser user = LagouUser.builder().email(email).password(password).build();
                userService.saveUser(user);

                // 保存用户令牌
                String token = UUID.randomUUID().toString();
                LagouToken lagouToken = LagouToken.builder().email(email).token(token).build();
                tokenService.saveToken(lagouToken);
                return 0;
            } else {
                log.warn("该邮箱已注册, email={}", email);
                return 1;
            }
        }
        return 2;
    }

    /**
     * 判断是否注册
     *
     * @param email
     * @return
     */
    @GetMapping("/isRegistered/{email}")
    public Boolean isRegistered(@PathVariable String email) {
        return userService.findOne(email) != null;
    }

    /**
     * 登录接口
     *
     * @param email
     * @param password
     * @return
     */
    @GetMapping("/login/{email}/{password}")
    public boolean login(@PathVariable String email, @PathVariable String password, HttpServletResponse response) {
        LagouUser userInfo = userService.findOne(email, password);
        if (userInfo == null) {
            log.error("该邮箱未注册或密码错误, email={}", email);
            return false;
        }

        LagouToken tokenInfo = tokenService.findOneByEmail(email);
        if (tokenInfo == null) {
            log.error("该注册用户无token");
            return false;
        }
        //创建Cookie
        Cookie cookie = new Cookie("token", tokenInfo.getToken());
        cookie.setPath("/");
        response.addCookie(cookie);
        return true;
    }

    /**
     * 根据token查询用户是否登录
     *
     * @param token
     * @return
     */
    @GetMapping("/info/{token}")
    public String emailInfo(@PathVariable String token) {
        LagouToken lagouToken = tokenService.findOne(token);
        return lagouToken == null ? "" : lagouToken.getEmail();
    }
}
